<?php

class WakFolder implements iWakFS, Iterator {

    /**
     * @param string $path
     */
    public function __construct($path) {
        $this->_path=$path;
    }

    /**
     * return given path
     * @return string
     */
    public function path() {
        return $this->_path;
    }

    /**
     * @return absolute path, without trailing slashes
     * @return string
     */
    public function realPath() {
        $path=realpath($this->path());
        $last=substr($path,-1,1);
        if ($last=='\\' or $last=='/')
            $path=substr($path,0,-1);
        return $path;
    }

    /**
     * does element exists ?
     * @return bool
     */
    public function exists() {
        return file_exists($this->path());
    }

	/**
	* is the folder readable ?
	* @access public
	* @return bool
	*/
	public function isReadable() {
		/*if (PHP_OS=='WINNT')
			return true;*/
		//clearstatcache();
		return is_readable($this->realpath());
	}/**/

	/**
	* is the folder writable ?
	* @return bool
	*/
	public function isWritable() {
		/*if (PHP_OS=='WINNT')
			return true;*/
		return is_writable($this->realpath());
	}

    /**
     * get dirname
     * @return string
     */
    public function dirname() {
        return dirname($this->_path);
    }

    /**
     * @return string
     */
    public function basename() {
        return basename($this->_path);
    }

	/**
	* creates dir if not exists
	* @param int $mode
	* @access public
	* @return bool
	*/
	public function create($mode=0777) {
		return mkdir($this->_path,$mode);
	}/**/

	/**
	* moves to new path
	* @param string $new_path
	* @access public
	* @return bool
	*/
	public function move($new_path) {
        if (!$this->exists())
            return false;
        // normalizing new path
        $new_path=realpath($new_path);
        $last=substr($new_path,-1,1);
        if ($last=='\\' or $last=='/')
            $new_path=substr($new_path,0,-1);

        $old=$this->realPath();
        $new=$new_path.'/'.$this->basename();
        // echo "<p>$old => $new</p>";
        if (!rename($old,$new))
            return false;
        $this->_path=$new;
        return true;
	}

    /**
     * renames folder (new basename within same folder)
     * @return bool
     */
    public function rename($name) {
        $old=$this->realPath();
        $dir=$this->dirname();
        $new=$dir?($dir.'/'.$name):$name; // echo "<p>$old => $new</p>";
        if (!rename($old,$new))
            return false;
        $this->_path=$new;
        return true;
    }

    /**
     * destroy element
     * @return bool
     */
    public function delete() {
        if (!$this->exists())
            return false;
        return rmdir($this->realpath());
    }

/*------------------------------------------------------------------------------
 Iterator methods
------------------------------------------------------------------------------*/
    public function current() {

    }

    public function next() {

    }

    public function key() {

    }

    public function rewind() {

    }

    public function valid() {

    }

    protected $_path;
}
